home *** CD-ROM | disk | FTP | other *** search
- #include <mac/quickdraw.h>
- #include <mac/toolintf.h>
- #include <mac/osintf.h>
- #include <mac/packintf.h>
- #include "ascii.h"
-
- main () {
- /* Standard initialisation */
- struct QDVar QDVar;
-
- QD = &QDVar;
- InitGraf (&thePort);
- InitFonts ();
- FlushEvents (everyEvent, 0);
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs ( (ProcPtr) 0);
- /* InitCursor (); Done in the LOOP */
-
- /* Application dependent initialisation */
- init_the_w ();
- init_the_text ();
- init_the_menu ();
- init_the_scrap ();
- init_the_cursor ();
- file_num = NONE;
-
- /* The Main Event LOOP */
- while (1){
- TEIdle (h_text);
- SystemTask ();
-
- VOID GetNextEvent (everyEvent, &my_event);
-
- if (the_w == FrontWindow ()){
- adjust_cursor ();
- mouse_p.h = my_event.where.h;
- mouse_p.v = my_event.where.v;
- GlobalToLocal (&mouse_p);
- }
-
- switch (my_event.what){
- case mouseDown:
- switch (FindWindow (&my_event.where, &which_w)){
- case inMenuBar:
- do_command (MenuSelect (&my_event.where));
- break;
- case inContent:
- if (the_w == FrontWindow ())
- the_w_click ();
- else
- SelectWindow (which_w);
- break;
- case inDrag:
- break; /* No drag done */
- case inGoAway:
- break; /* Ne exit availible */
- case inSysWindow:
- SystemClick (&my_event, which_w);
- break;
- }
- break;
- case autoKey:
- case keyDown:
- if (the_w == FrontWindow ())
- do_key ();
- break;
- case activateEvt:
- if (the_w == (WindowPtr) my_event.message){
- TEActivate (h_text);
- DisableItem (my_menus[2], TEUNDO);
- EnableItem (my_menus[3], 0);
- EnableItem (my_menus[4], 0);
- }
- else{
- TEDeactivate (h_text);
- EnableItem (my_menus[2], TEUNDO);
- DisableItem (my_menus[3], 0);
- DisableItem (my_menus[4], 0);
- }
- DrawMenuBar ();
- break;
- case updateEvt:
- if (the_w == (WindowPtr) my_event.message){
- BeginUpdate (the_w);
- SetCursor (*watch_c);
- draw_the_w ();
- TEUpdate (&text_r, h_text);
- ShowCursor ();
- EndUpdate (the_w);
- }
- break;
- }
- }
- }
-
- init_the_w (){
- int x, y;
-
- the_w = GetNewWindow (WINDOW, &w_record, (WindowPtr) - 1);
- SetRect (&ascii_r, 18, 18, 418, 320);
- SetRect (&text_r, 425, 18, 500, 317);
- SetRect (&view_r, 420, 0, 512, 322);
- for (x = 0; x < 16; x++)
- for (y = 0; y < 16; y++)
- SetRect (&char_r[x][y], 18+25*x, 18+19*y, 43+25*x, 37+19*y);
- SetPort (the_w);
- }
-
- init_the_text (){
- char temp_str[256];
-
- h_text = TENew (&text_r, &view_r); /* view_r > text_r */
- h_authortext = TENew (&text_r, &view_r);
- sample_str = GetString(SAMPLESTR);
- author_str = GetString(AUTHORSTR);
- p2c_string(*sample_str, temp_str);
- TESetText (temp_str, (int) (*sample_str)[0], h_text);
- p2c_string(*author_str, temp_str);
- TESetText (temp_str, (int) (*author_str)[0], h_authortext);
- }
-
- change_font (size) int size;{ /* 0 means new font => chose smallest */
- int font, i;
- TEHandle TEnew_h;
- Handle save_h;
- short int save_length,save_start, save_end;
-
- for (i = 1; i <= num_of_sizes; i++)
- CheckItem (my_menus[4], i, 0);
- GetItem (my_menus[3], which_font, font_name);
- GetFNum (font_name, &font);
- if (! size){
- for (i = 1; i <= num_of_sizes; i++)
- if (RealFont (font, font_sizes[i])){
- size = font_sizes[i];
- break;
- }
- if (size > 18)
- size = 12;
- }
- if (! size)
- size = 12;
-
- TextFont(font);
- TextSize(size);
-
- save_h = (*h_text)->hText; /* Save handle to text */
- save_length = (*h_text)->teLength;
- save_start = (*h_text)->selStart;
- save_end = (*h_text)->selEnd;
-
- TEnew_h = TENew(&text_r, &view_r); /* Create new record */
-
- (*h_text)->hText = (*TEnew_h)->hText; /* Change handle in old record */
- TEDispose(h_text); /* Dispose of old record */
-
- h_text = TEnew_h; /* Rename new record */
-
- (*h_text)->hText = save_h; /* Restore handle to text */
- (*h_text)->teLength = save_length;
- (*h_text)->selStart = save_start;
- (*h_text)->selEnd = save_end;
-
- TEActivate(h_text);
- TECalText (h_text);
-
- for (i = 1; i <= num_of_sizes; i++)
- if (the_w->txSize == font_sizes[i])
- CheckItem (my_menus[4], i, 1);
- }
-
- init_the_menu (){
- my_menus[0] = GetMenu(APPLE);
- (**my_menus[0]).menuData.s[1] = '\024'; /* Silly thing this !! */
- AddResMenu (my_menus[0], "DRVR");
- InsertMenu (my_menus[0], 0);
-
- my_menus[1] = GetMenu (FILE);
- InsertMenu (my_menus[1], 0);
-
- my_menus[2] = GetMenu (EDIT);
- InsertMenu (my_menus[2], 0);
-
- add_font_menu ();
-
- DrawMenuBar ();
- }
-
- add_font_menu (){
- my_menus[3] = NewMenu (FONTS, "Font");
- AddResMenu (my_menus[3], "FONT");
- InsertMenu (my_menus[3], 0);
- which_font = 1;
- CheckItem (my_menus[3], which_font, 1);
- change_font (12);
- add_size_menu ();
- change_font (0);
- }
-
- add_size_menu (){
- char name[64];
- int i;
-
- my_menus[4] = NewMenu (FONTSIZE, font_name);
-
- num_of_sizes = 0;
- for (i = 1; i < 128; i++)
- if (RealFont (the_w->txFont, i)){
- NumToString (i, name);
- append_string ("<O", name);
- AppendMenu (my_menus[4], name);
- font_sizes[++num_of_sizes] = i;
- }
- else
- if (i == 9 || i == 10 || i == 12 || i == 14 || i == 18){
- NumToString (i, name);
- AppendMenu (my_menus[4], name);
- font_sizes[++num_of_sizes] = i;
- }
- InsertMenu (my_menus[4], 0);
- for (i = 1; i <= num_of_sizes; i++)
- if (the_w->txSize == font_sizes[i])
- CheckItem (my_menus[4], i, 1);
- }
-
- append_string (app, str) char *app, *str;{
- int i, j;
-
- i = 0;
- while (str[i++] != 0 && i < 60);
- i--;
-
- j = 0;
- while (app[j] != 0 && i + j < 60)
- str[i++] = app[j++];
- str[i] = 0;
- }
-
- init_the_cursor (){
- finger_c = GetCursor (FINGER); HNoPurge ((Handle) finger_c);
- watch_c = GetCursor (WATCH); HNoPurge ((Handle) watch_c);
- bar_c = GetCursor (BAR); HNoPurge ((Handle) bar_c);
- }
-
- init_the_scrap (){
- ScrapStuff *scrap_stuff;
-
- scrap_stuff = InfoScrap ();
- scrap_count = ! ( scrap_stuff->scrapCount );
- /* get_scrap (); Dosn't work yet */
- }
-
- do_key (){
- char the_char;
-
- the_char = my_event.message & 0xff;
- if(my_event.modifiers & cmdKey)
- do_command(MenuKey(the_char));
- else{
- InvertRect (&char_r[(the_char>>4) & 0xf][the_char & 0xf]);
- TEKey (the_char, h_text);
- InvertRect (&char_r[(the_char>>4) & 0xf][the_char & 0xf]);
- }
- }
-
- draw_the_w (){
- int x, y, save_font, save_size;
-
- EraseRect (&the_w->portRect);
- FrameRect (&ascii_r);
-
- for (x = 0; x < 16; x++){
- save_font = the_w->txFont;
- save_size = the_w->txSize;
- the_w->txFont = systemFont;
- the_w->txSize = 12;
- MoveTo (5, char_r[0][x].top + 14);
- if (x < 10)
- DrawChar ( (char) ( x + '0') );
- else
- DrawChar ( (char) ( x + 'a' - 10) );
- MoveTo (char_r[x][0].topLeft.h + 5, 10);
- if (x < 10)
- DrawChar ( (char) ( x + '0') );
- else
- DrawChar ( (char) ( x + 'a' - 10) );
- the_w->txFont = save_font;
- the_w->txSize = save_size;
- for (y = 0; y < 16; y++){
- MoveTo (char_r[x][y].topLeft.h + 5, char_r[x][y].top + 14);
- DrawChar ( (char) ( x*16 + y ) );
- }
- }
- }
-
- do_command (help) long help;{
- char name[64];
-
- the_menu = HiWord (help);
- the_item = LoWord (help);
- switch (the_menu){
- case APPLE:
- if(the_item == ABOUT){
- about();
- break;
- }
- if(the_item == ABOUT + 1)
- break;
- GetItem (my_menus[0], the_item, name);
- {
- GrafPtr save_port;
- char *da_name ();
-
- GetPort (&save_port);
- if (GetNamedResource ("DRVR", name))/* Without leading zero */
- VOID OpenDeskAcc (name);
- else /* With leading zero */
- VOID OpenDeskAcc (da_name (name));
- SetPort (save_port);
- }
- break;
- case FILE:
- switch (the_item){
- case OPEN1:
- case OPEN2:
- case OPEN3:
- open_file (the_item);
- break;
- case QUIT:
- VOID LoadScrap ();
- ExitToShell ();
- break;
- }
- break;
- case EDIT:
- if (SystemEdit (the_item - 1))
- break;
- switch (the_item){
- case TECUT:
- TECut (h_text);
- put_scrap ();
- break;
- case TECOPY:
- TECopy (h_text);
- put_scrap ();
- break;
- case TEPASTE:
- get_scrap ();
- TEPaste (h_text);
- break;
- case TECLEAR:
- TEDelete (h_text);
- break;
- }
- break;
- case FONTS:
- CheckItem (my_menus[3], which_font, 0);
- which_font = the_item;
- change_font (0);
- CheckItem (my_menus[3], which_font, 1);
-
- num_of_sizes = 0;
- DeleteMenu (FONTSIZE);
- add_size_menu ();
- DrawMenuBar ();
-
- InvalRect (&the_w->portRect);
- break;
- case FONTSIZE:
- change_font (font_sizes[the_item]);
- InvalRect (&the_w->portRect);
- break;
- }
- HiliteMenu (0);
- }
-
- adjust_cursor (){
- if (PtInRect (&mouse_p, &ascii_r)){
- SetCursor (*finger_c);
- return;
- }
- if (PtInRect (&mouse_p, &text_r)){
- SetCursor (*bar_c);
- return;
- }
- InitCursor ();
- }
-
- about(){
- EraseRect (&text_r);
- TEUpdate (&text_r, h_authortext);
- while (! Button ());
- while (Button ());
- EraseRect (&text_r);
- TEUpdate (&text_r, h_text);
- FlushEvents (everyEvent, 0);
- }
-
- the_w_click (){
- int x, y;
-
- if (PtInRect (&mouse_p, &ascii_r)){
- x = (mouse_p.h - 18) / 25;
- y = (mouse_p.v - 18) / 19;
- InvertRect (&char_r[x][y]);
- TEKey ( (char) ( x*16 + y ) , h_text);
- InvertRect (&char_r[x][y]);
- }
- if (PtInRect (&mouse_p, &text_r))
- if(my_event.modifiers & shiftKey)
- TEClick (&mouse_p, 1, h_text);
- else
- TEClick (&mouse_p, 0, h_text);
- }
-
- put_scrap (){
- ScrapStuff *scrap_stuff;
-
- VOID ZeroScrap ();
- HLock (TEScrapHandle);
- VOID PutScrap (GetHandleSize (TEScrapHandle), "TEXT", *TEScrapHandle);
- HUnlock (TEScrapHandle);
- scrap_stuff = InfoScrap ();
- scrap_count = scrap_stuff->scrapCount;
- }
-
- get_scrap (){
- int off, len;
- ScrapStuff *scrap_stuff;
-
- scrap_stuff = InfoScrap ();
- if (scrap_count == scrap_stuff->scrapCount)
- return;
- scrap_count = scrap_stuff->scrapCount;
- len = GetScrap (TEScrapHandle, "TEXT", &off);
- if (len >= 0)
- TEScrapLen = len;
- /* Else, it was probably a PICT or other type of scrap;
- in this case TEScrapHandle's contents has not been touched. */
- }
-
- open_file (item) int item;{
- Point top_left;
- int file_filter1(), file_filter2();
- OsType file_type;
-
- SetPt (&top_left, 50, 50);
- switch(item){
- case OPEN1:
- SFGetFile (&top_left, "", file_filter1, -1,
- (char *) 0, (ProcPtr) 0, &file_record);
- break;
- case OPEN2:
- file_type.s[0] = 'F';
- file_type.s[1] = 'F';
- file_type.s[2] = 'I';
- file_type.s[3] = 'L';
- SFGetFile (&top_left, "", file_filter1, 1,
- (char *) &file_type, (ProcPtr) 0, &file_record);
- break;
- case OPEN3:
- SFGetFile (&top_left, "", file_filter2, -1,
- (char *) 0, (ProcPtr) 0, &file_record);
- break;
- }
- if (file_record.good){
- if (file_num != NONE){
- VOID SetVol ((char *) 0, volume_num);
- CloseResFile (file_num);
- }
- volume_num = file_record.vRefNum;
- VOID SetVol ((char *) 0, volume_num);
- p2cstr ((char *) file_record.fName);
- file_num = OpenResFile (file_record.fName);
- DeleteMenu (FONTS);
- DeleteMenu (FONTSIZE);
- add_font_menu ();
- DrawMenuBar ();
- InvalRect (&the_w->portRect);
- }
- }
-
- /* This filter dosn't open System file, Ascii Application file and
- files without resource fork. */
-
-
- file_filter1() {
- struct pargs {
- FileParam *paramBlock;
- } pargs;
- char *retval= (char *) getpargs(&pargs, sizeof pargs);
- if (pargs.paramBlock->ioFlRLgLen == 0)
- *retval= 1; /* TRUE => skip this file */
- else{
- char fdType[5], fdCreator[5];
-
- Os2c_string(pargs.paramBlock->ioFlFndrInfo.fdType, fdType);
- Os2c_string(pargs.paramBlock->ioFlFndrInfo.fdCreator, fdCreator);
- if(EqualString("ZSYS", fdType, 0, 0) &&
- EqualString("MACS", fdCreator, 0, 0) ||
- EqualString("APPL", fdType, 0, 0) &&
- EqualString("AscI", fdCreator, 0, 0))
- *retval= 1; /* TRUE => skip this file */
- else
- *retval= 0; /* FALSE => show this file*/
- }
- }
-
- /* This filter dosn't open System file, Ascii Application file,
- files without resource fork and files whithout FONT resource. */
-
-
- file_filter2() {
- struct pargs {
- FileParam *paramBlock;
- } pargs;
- char *retval= (char *) getpargs(&pargs, sizeof pargs);
- if (pargs.paramBlock->ioFlRLgLen == 0)
- *retval= 1; /* TRUE => skip this file */
- else{
- char fdType[5], fdCreator[5];
-
- Os2c_string(pargs.paramBlock->ioFlFndrInfo.fdType, fdType);
- Os2c_string(pargs.paramBlock->ioFlFndrInfo.fdCreator, fdCreator);
- if(EqualString("ZSYS", fdType, 0, 0) &&
- EqualString("MACS", fdCreator, 0, 0) ||
- EqualString("APPL", fdType, 0, 0) &&
- EqualString("AscI", fdCreator, 0, 0))
- *retval= 1; /* TRUE => skip this file */
- else{
- int num_resources, f_num;
- char f_name[256];
-
- VOID SetVol ((char *) 0, pargs.paramBlock->ioVRefNum);
- num_resources = CountResources("FONT");
- p2c_string((char *) pargs.paramBlock->ioNamePtr, f_name);
- f_num = OpenResFile (f_name);
- if(CountResources("FONT") > num_resources)
- *retval= 0; /* FALSE => show this file*/
- else
- *retval= 1; /* TRUE => skip this file */
- CloseResFile(f_num);
- }
- }
- }
-
- /*
- * Some desk accessories have weird names that begin with a leading zero.
- * Since this fools the automatic C/Pascal string conversion stuff, we
- * have this little gem; given a C string, it returns a Pascal string
- * with leading zero.
- */
- char *da_name (s) char *s; {
- static char ps[32];
- int i = 1;
- register char *cp,*dp;
-
- cp = s;
- dp = &ps[2];
- while ((*dp++ = *cp++))
- i++;
- ps[0] = i;
- return (isapstr (ps));
- }
-
- /*
- * Copy a Pascal string into a buffer, converting it into a C string.
- */
- p2c_string(pSource, cDestination)
- char *cDestination;
- char *pSource;
- {
- int i;
-
- for (i = 0; i < pSource[0]; ++i)
- cDestination[i] = pSource[i + 1];
- cDestination[i] = '\0';
- }
-
- /*
- * Copy a OsType string into a buffer, converting it into a C string.
- */
- Os2c_string(OsSource, cDestination)
- char *cDestination;
- OsType OsSource;
- {
- int i;
-
- for (i = 0; i < 4; ++i)
- cDestination[i] = OsSource.s[i];
- cDestination[4] = '\0';
- }
-